# Copyright (C) 2010 Razvan Constantin <razvan.constantin@rconst.net>
#
# This file is part of FileBow.
#
# FileBow is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FileBow is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FileBow.  If not, see <http:#www.gnu.org/licenses/>.

# WAF builder extensions customized for filebow

import Options

def library(self, lib):
  self.lib = lib
  self.lib_objs = []

def abs_target_name(self, name):
  if '#' not in name:
    return self.lib + '#' + name
  return name

def build_target(self, name, deps = (), libs = ('POCO', ), test = False):
  libname = self.abs_target_name(name)
  local_deps = ' '.join([self.abs_target_name(dep) for dep in deps])
  self.new_task_gen(
      features = 'cxx cstaticlib',
      name = libname,
      target = libname,
      source = [ name + '.cpp' ],
      includes = self.srcnode.abspath(),
      uselib = ' '.join(libs),
      uselib_local = local_deps,
      install_path = None)
  if test and Options.options.test:
    testname = libname + '.test'
    self.new_task_gen(
      features = 'cxx cprogram crun',
      name = testname,
      target = testname,
      source = [ name + '_test.cpp' ],
      includes = self.srcnode.abspath(),
      uselib_local = libname + ' testing#test_main testing#test_drive ' + local_deps,
      install_path = None)

def build_plugin(self, name, main, deps):
  """ Builds the plugin shared library to be copied in the Geany plugins folder """
  env = self.env
  plugin_env = env.copy()
  plugin_env['shlib_PATTERN'] = '%s'
  self.set_env('default', plugin_env)
  self.new_task_gen(
      features = 'cxx cshlib',
      name = name,
      target = '%s.so' % name,
      source = [ main ],
      uselib = 'GEANY GTK POCO',
      uselib_local = ' '.join(deps),
      install_path = self.env['GEANY_PLUGINS'])
  self.set_env('default', env)

def extend(bld):
  import types
  for fn in (abs_target_name, library, build_target, build_plugin):
    setattr(bld, fn.__name__, types.MethodType(fn, bld))
